package com.game.controller;

import com.game.data.BagData;
import com.game.data.RoleData;
import com.game.model.Bag;
import com.game.model.goods.Goods;
import com.game.model.role.NPC;
import com.game.model.role.Role;
import com.game.model.task.Task;
import com.game.utils.Format;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @Description:任务管理器
 **/
public class TaskController {
    //判断任务是否已经接取
    public static boolean isGet(Task task) {
        if (task == null) return false;
        if (RoleData.taskMap == null) return false;
        Iterator<Map.Entry<Integer, Task>> iterator = RoleData.taskMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, Task> next = iterator.next();
            //如果在任务列表里有这个任务表示已接取
            if (next.getValue().equals(task)) {
                return true;
            }
        }
        return false;
    }

    //判断任务是否完成
    public static boolean isDone(Task task) {
        if (task.getFightTask() != null) {
            Iterator<Map.Entry<Role, int[]>> iterator = task.getFightTask().entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Role, int[]> next = iterator.next();
                int[] value = next.getValue();
                //如果数量不够
                if (value[0] < value[1])
                    return false;
            }
        }
        //标记背包拥有各个任务物品的个数
        int flag = 0;
        if (task.getGoodsTask() != null) {
            Iterator<Map.Entry<Goods, Integer>> iterator = task.getGoodsTask().entrySet().iterator();
            Iterator<Map.Entry<Goods, Integer>> iterator1 = BagData.bag.getGoodsMap().entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Goods, Integer> next = iterator.next();
                while (iterator1.hasNext()) {
                    Map.Entry<Goods, Integer> next1 = iterator1.next();
                    //如果背包里有任务要的物品，比较大小
                    if (next.getKey().equals(next1.getKey())) {
                        //如果拥有的数量小于需要的（不满足）
                        if (next1.getValue() < next.getValue()) {
                            return false;
                        } else {
                            //标记一下满足条件的物品
                            flag++;
                        }
                    }
                }
            }
            //如果满足次数和需要的任务物品种类一样
            if (flag == task.getGoodsTask().size()) {
                return true;
            } else {
                return false;
            }
        }
        return true;
    }

    //接取任务
    public static boolean getTask(Task task) {
        if (RoleData.taskMap == null) RoleData.taskMap = new HashMap<>();
        //如果任务列表没有就直接添加该任务
        if (RoleData.taskMap.size() <= 0) {
            RoleData.taskMap.put(task.getId(), task);
            return true;
        }
        Iterator<Map.Entry<Integer, Task>> iterator = RoleData.taskMap.entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Integer, Task> next = iterator.next();
            //如果已经接了该任务
            if (next.getKey() == task.getId()) {
                return false;
            }
            {
                //添加新任务
                RoleData.taskMap.put(task.getId(), task);
                return true;
            }
        }
        return false;
    }

    //提交任务(除去背包物品,清除NPC上对应任务)
    public static boolean submit(Task task) {
        //判断任务是否完成
        if (isDone(task)) {
            System.out.println(111);
            //移除背包物品
            boolean result = BagData.bag.removeToBag(task.getGoodsTask());
            if (result) {
                System.out.println(222);
                //获取奖励
                if (BagData.bag.addToBag(task.getRewards())) {
                    System.out.println(333);
                    //清除npc上的任务
                    if (removeTaskFormNPC(task)) {
                        System.out.println(4444);
                        //清除自身的任务表中任务
                        if (removeTaskFormOwner(task)){
                            System.out.println(555);
                            return true;
                        }else {
                            System.out.println("自身任务表任务移除失败！！！");
                            return false;
                        }
                    } else {
                        System.out.println("NPC任务移除失败异常！！！");
                        return false;
                    }
                } else {
                    System.out.println("获取奖励失败！！！");
                    return false;
                }
            } else {
                System.out.println("移除物品失败！！！");
            }
        }
        return false;
    }

    //移除自身任务表上的任务
    private static boolean removeTaskFormOwner(Task task) {
        if (RoleData.taskMap==null||RoleData.taskMap.size()<=0)return false;
        Iterator<Map.Entry<Integer, Task>> iterator = RoleData.taskMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, Task> next = iterator.next();
            if (next.getKey()==task.getId()){
                RoleData.taskMap.remove(next.getKey());
                return true;
            }
        }
        return false;
    }
    //移除NPC上的任务
    private static boolean removeTaskFormNPC(Task task){
        System.out.println(task);
        if (task==null)return false;
        System.out.println(111);
        int flag = 0;
        Iterator<Map.Entry<Integer, NPC>> iterator = RoleData.npcMap.entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Integer, NPC> next = iterator.next();
            if (next.getValue().getTaskMap()!=null){
                System.out.println(222);
                Iterator<Map.Entry<Integer, Task>> iterator1 = next.getValue().getTaskMap().entrySet().iterator();
                while (iterator1.hasNext()){
                    System.out.println(333);
                    Map.Entry<Integer, Task> next1 = iterator1.next();
                    //任务NPC身上有该任务
                    System.out.println(next1.getKey()+"--"+task.getId());
                    if (next1.getKey()==task.getId()){
                        System.out.println(444);
                        next.getValue().getTaskMap().remove(next1.getKey());
                        flag++;
                    }
                }
            }
        }
        return flag!=0;
    }
    //打印任务需求
    public static String printTaskNeed(Task task) {
        String str = "";
        if (task.getFightTask() != null) {
            Iterator<Map.Entry<Role, int[]>> iterator = task.getFightTask().entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Role, int[]> next = iterator.next();
                str += "【" + next.getKey().getName() + "】" + "[" + next.getValue()[0] + "/" + next.getValue()[1] + "]\t";
            }
        }
        if (task.getGoodsTask() != null) {
            Iterator<Map.Entry<Goods, Integer>> iterator = task.getGoodsTask().entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Goods, Integer> next = iterator.next();
                str += "【" + next.getKey().getName() + "】" + "[" +getNum(next.getKey())+"/"+next.getValue()+"]\t";
            }
        }
        return str==""?"无道具需要":str;
    }

    //打印任务奖励
    public static String printTaskRewards(Task task){
        String str = "";
        if (task.getRewards()==null||task.getRewards().size()<=0)return "无任务奖励";
        Iterator<Map.Entry<Goods, Integer>> iterator = task.getRewards().entrySet().iterator();
        while (iterator.hasNext()){
            Map.Entry<Goods, Integer> next = iterator.next();
            str += Format.getQuality(next.getKey().getQuality())+"·【"+next.getKey().getName()+"】X"+next.getValue()+"\t";
        }
        return str;
    }
    //获取背包里某物品数量
    private static int getNum(Goods goods) {
        Iterator<Map.Entry<Goods, Integer>> iterator = BagData.bag.getGoodsMap().entrySet().iterator();
        while (iterator.hasNext()) {
            Map.Entry<Goods, Integer> next = iterator.next();
            if (next.getKey().equals(goods)){
                return next.getValue();
            }
        }
        return 0;
    }
}
